package com.folioreader.ui.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.GestureDetectorCompat;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.folioreader.R;

public class WebViewPagerNew extends ViewPager {
    int horizontalPageCount = 0;
    private FolioWebViewNew folioWebViewNew= null;
    private boolean takeOverScrolling= false;
    boolean isScrolling= false;
    private Handler uiHandler= null;
    private GestureDetectorCompat gestureDetectorCompat= null;
    private LastGestureType lastGestureType= null;

    private enum  LastGestureType{
        OnSingleTapUp, OnLongPress, OnFling, OnScroll
    }

    public WebViewPagerNew(@NonNull Context context) {
        super(context);
    }

    public boolean isScrolling() {
        return isScrolling;
    }


    public WebViewPagerNew(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init(){
        uiHandler= new Handler();
        gestureDetectorCompat= new GestureDetectorCompat(getContext(),new GestureListener());

        addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                isScrolling= true;
                if(takeOverScrolling&&folioWebViewNew!=null){
                    int scrollX= folioWebViewNew.getScrollXPixelsForPage(position)+positionOffsetPixels;
                    folioWebViewNew.scrollTo(scrollX, 0);
                }
                if(positionOffsetPixels == 0){
                    takeOverScrolling= false;
                    isScrolling= false;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void setHorizontalPageCount(int horizontalPageCount){
        this.horizontalPageCount= horizontalPageCount;
        setAdapter(new WebViewPagrAdapter());
        if(folioWebViewNew== null){
            folioWebViewNew = ((View) getParent()).findViewById(R.id.folioWebView);
        }
    }
    @JavascriptInterface
    public void setCurrentPage(final int pageIndex){
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
            setCurrentItem(pageIndex,false);
            }
        });
    }
    @JavascriptInterface
    public void setPageToLast(){
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
            setCurrentItem(horizontalPageCount-1);
            }
        });
    }
    @JavascriptInterface
    public void setPageToFirst(){
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                setCurrentItem(0);
            }
        });
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            WebViewPagerNew.super.onTouchEvent(e);
            return true;
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            lastGestureType= LastGestureType.OnSingleTapUp;
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
            lastGestureType=LastGestureType.OnLongPress;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            lastGestureType= LastGestureType.OnScroll;
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            lastGestureType=LastGestureType.OnFling;
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev==null){
            return false;
        }
        if(gestureDetectorCompat == null){
            return false;
        }
        boolean gestureReturn= gestureDetectorCompat.onTouchEvent(ev);
        if(gestureReturn){
            return true;
        }
        boolean superReturn = super.onTouchEvent(ev);

        if(ev.getAction() == MotionEvent.ACTION_UP){
            if(lastGestureType==LastGestureType.OnScroll||lastGestureType == LastGestureType.OnFling){
                takeOverScrolling= true;
            }

            lastGestureType =null;

        }
        return superReturn;
    }
    private class WebViewPagrAdapter extends PagerAdapter{
        @Override
        public int getCount() {
            return horizontalPageCount;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = LayoutInflater.from(container.getContext()).inflate(R.layout.view_webview_pager,container,false);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }
}
